package com.jxtti.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.Sys_TypeID;
import com.jxtti.service.SysTypeIDService;
import com.jxtti.utils.ExceptionUtil;

@Controller
@RequestMapping("/SysTypeID") 
public class SysTypeIDController {
	private final static Logger log= Logger.getLogger(SysTypeIDController.class);
	
	@Autowired 
	private SysTypeIDService  sysTypeIDService; 

	
	@InitBinder 
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }		
	
	@RequestMapping(value = "/listInterface")
	@ResponseBody
	public ModelAndView listInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/Sys/TypeID/list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	/**
	 *  <!--用户列表 分页     -->
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/list", method = RequestMethod.POST) 
	@ResponseBody
	public Map<String, Object>  list(@RequestParam Map<String, Object> data,  Sys_TypeID sys_TypeID, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		Map<String, Object> result = new HashMap<String, Object>();			
		PageInfo<Sys_TypeID> page = sysTypeIDService.list(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		return result;	
	}
	
	
	@RequestMapping("/queryAll")
	@ResponseBody
	public List<Sys_TypeID> queryTCityAQIWriteAll(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{	
		
		List<Sys_TypeID>  sysTypeID= sysTypeIDService.queryAll(data);
		return sysTypeID;
	}
	
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/Sys/TypeID/edit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}	
	
	@RequestMapping("/save")
	@ResponseBody	
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			Sys_TypeID  Sys_TypeID, HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {				
			int exeCount = sysTypeIDService.add(Sys_TypeID);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}	
	
	
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request,Sys_TypeID  sysTypeID,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			sysTypeID = sysTypeIDService.queryListById(sysTypeID);		
			data.put("sys_TypeID", sysTypeID);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/Sys/TypeID/edit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
		
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			Sys_TypeID  sysTypeID, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			int exeCount = sysTypeIDService.update( sysTypeID);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
		
	
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) {
		try {			
			String[] idArr=data.get("ids").toString().split(",");
			for(String id:idArr){
				Sys_TypeID  sysTypeID=new Sys_TypeID();
				sysTypeID.setId(Integer.parseInt(id));								
				int exeCount=sysTypeIDService.delete(sysTypeID);
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
}
